/**
 * chenuu.com
 * 
 * @author  zhaoxu.com@gmail.com
 * @created 2012-11-15
 */
package com.chenuu.common.config.value;

import java.lang.reflect.Method;

import com.chenuu.common.exceptions.ChenuuException;

/**
 *
 */
public abstract class AbstractValueFactory<T> implements ValueFactory {

	/* (non-Javadoc)
	 * @see com.chenuu.common.config.ValueFactory#setValue(java.lang.Object, java.lang.String, java.lang.String)
	 */
	@Override
	public void setValue(Object object, String fieldName, String stringValue) {
		try {
			Method m = object.getClass().getMethod(getterMethodName(fieldName), getValueType());
			m.invoke(object, getValue(stringValue));
		} catch (Exception e) {
			throw new ChenuuException("赋值失败。", e);
		}
	}
	
	private String getterMethodName(String name) {
		String first = name.substring(0, 1);
		String other = name.substring(1);
		return "set" + first.toUpperCase() + other;
	}

	protected abstract T getValue(String stringValue);

	protected abstract Class<T> getValueType();

}
